package com.xyz.service;

import com.free.common.vo.ResultModel;
import com.xyz.dao.logic.CCustomersmsLogic;
import com.xyz.domain.constants.EhCacheConstants;
import com.xyz.domain.constants.SysConstants;
import com.xyz.domain.table.CCustomersms;
import com.xyz.interceptor.QueueCacheInfo;
import com.xyz.sms.SmsUtils;
import com.xyz.utils.RegexUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Random;

@Service
public class CommonService extends BaseService{


    @Autowired
    private CCustomersmsLogic custSmsLogic;


    @Value(value = "${sys.status}")
    private String sysStatus;

    private static final String SMS_PHONE_FORMAT = "SMS_PHONE_%s";



    @QueueCacheInfo
    public ResultModel smsValidateNo(String phone, Integer type){
        if(StringUtils.isEmpty(phone)){
            return ResultModel.failure(null,"手机号码必须输入");
        }
        if(!RegexUtils.isMobileSimple(phone)){
            return ResultModel.failure(null,"请输入正确的手机号码");
        }
        String smsCode= getStringFromQueue(EhCacheConstants.SmsPhoneNumCache, phone);
        if(StringUtils.isNotEmpty(smsCode)){
            if(StringUtils.endsWithIgnoreCase(sysStatus, SysConstants.SYSTEM_STATUS_DEBUG)){
                return ResultModel.success(smsCode);
            }else{
                return ResultModel.failure(null,"手机验证码还在有效期");
            }
        }
        smsCode = smsCode(6);
        Map smsRes = sendSms(type, phone, smsCode);
        if(smsRes != null && smsRes.get("success").toString().equals("1")){
            CCustomersms smsInfo = new CCustomersms();
            smsInfo.setPhone(phone);
            smsInfo.setTimes(10);
            smsInfo.setCode(smsCode);
            int res = custSmsLogic.insertPhoneTodayInfo(smsInfo);
            putStringToQueue(EhCacheConstants.SmsPhoneNumCache, phone, smsCode);
            if(res > 0){
                if(StringUtils.endsWithIgnoreCase(sysStatus, SysConstants.SYSTEM_STATUS_DEBUG)){
                    return ResultModel.success(smsCode);
                }
                return ResultModel.success();
            }
        }
        return ResultModel.failure(null,"手机验证码发送失败");
    }

    private static String smsCode(int length){
        Random random = new Random();
        int i = 0;
        StringBuffer strBuffer = new StringBuffer();
        while(i<length){
           strBuffer.append(String.valueOf(random.nextInt(10)));
            i++;
        }
        return strBuffer.toString();
    }

    private Map<String,String> sendSms(Integer type, String phone, String message){
        if(type == null || type == 1){
            return SmsUtils.getInstance().sender().send(phone,message);
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(smsCode(6));
    }
}
